package com.cn.jdia.ets.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.joda.time.DateTime;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;

@Controller
public class ThreeQuota {

	@RequestMapping("/quodata")
	public String getQuota(HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		// System.out.println("Three Quota...");
		@SuppressWarnings("resource")
		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("springmvc.xml");
		DataSource ds = (DataSource) w_context.getBean("dataSource");

		Map<String, Integer> map = new HashMap<String, Integer>();

		DateTime datetime = new DateTime();
		String nowDate = datetime.toString("YYYY-MM-dd");
		String hajime = datetime.toString("YYYY") + "-01-01";

		String last = datetime.minusYears(1).toString("YYYY-MM-dd");
		String lasthajime = datetime.minusYears(1).toString("YYYY") + "-01-01";

		// System.out.println(hajime + " " + nowDate);
		// System.out.println();
		// System.out.println(lasthajime + " " + last);
		// System.out.println();

		try {
			Connection conn = ds.getConnection();
			Statement st = conn.createStatement();

			StringBuffer sb = new StringBuffer(
					"select sum(field0002) jiaci,sum(field0003) lvke,sum(field0004) huoyou from dbo.formmain_3095 where field0001>='")
							.append(hajime).append("' and field0001<='").append(nowDate).append("'");
			ResultSet rs = st.executeQuery(sb.toString());

			int jiaci = 0;
			int lvke = 0;
			int huoyou = 0;

			while (rs.next()) {
				jiaci = rs.getInt("jiaci");
				lvke = rs.getInt("lvke");
				huoyou = rs.getInt("huoyou");
			}

			// System.out.println(jiaci + " , " + lvke + " , " + huoyou);

			map.put("jiaci", jiaci);
			map.put("lvke", lvke);
			map.put("huoyou", huoyou);

			StringBuffer sb2 = new StringBuffer(
					"select sum(field0002) jiaci,sum(field0003) lvke,sum(field0004) huoyou from dbo.formmain_3095 where field0001>='")
							.append(lasthajime).append("' and field0001<='").append(last).append("'");
			ResultSet rs2 = st.executeQuery(sb2.toString());

			int l_jiaci = 0;
			int l_lvke = 0;
			int l_huoyou = 0;

			while (rs2.next()) {
				l_jiaci = rs2.getInt("jiaci");
				l_lvke = rs2.getInt("lvke");
				l_huoyou = rs2.getInt("huoyou");
			}

			// System.out.println(l_jiaci + " , " + l_lvke + " , " + l_huoyou);

			map.put("l_jiaci", l_jiaci);
			map.put("l_lvke", l_lvke);
			map.put("l_huoyou", l_huoyou);

			
			rs.close();
			rs2.close();
			
			st.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		Gson gson = new Gson();
		String json = gson.toJson(map);

		out.print(json);
		out.flush();
		out.close();

		return null;
	}
}
